import {Component} from 'react';
import {withRouter} from 'react-router-dom';
import {connect} from 'react-redux';
import {Storage} from "../../util";
import * as actionCreators from './store/actions';

class Auth extends Component{
  render(){
    return null;
  }
  componentDidMount(){
    const token = Storage.getKey('token');
    const pathname = this.props.location.pathname;
    if(!token){
      this.props.setLoginStatus(false);
      if(!pathname.includes('login') && !pathname.includes('reg')){
        this.props.setPathname(pathname);
        this.props.history.push('/login');
      }
    }else{
      this.props.setLoginStatus(true);
    }
  }
}

const mapDispatchToProps = dispatch => ({
  setLoginStatus(status){
    dispatch(actionCreators.setLoginStatus(status))
  },
  setPathname(pathname){
    dispatch(actionCreators.setPathName(pathname))
  }
});

export default connect(null, mapDispatchToProps)(withRouter(Auth));
